package com.foreknow.eldclient.dao;

import com.foreknow.eldclient.po.Business;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface BusinessMapper {

    /**
     * 根据商家ID查询商家信息
     * 直接使用注释不通过xml文件
     * @param businessId
     * @return
     */
    @Select(" select * from Business where businessId=#{businessId}")
    Business getBusinessById(int businessId);

    /**
     * 根据订单种类ID查询商家信息
     * @param OrderTypeId
     * @return
     */
    @Select("select * from Business where OrderTypeId=#{OrderTypeId}")
    List<Business> listBusinessByOrderTypeId(int OrderTypeId);

    /**
     * 查询所有商家
     * @return
     */
    @Select("select * from business")
    List<Business> listAllBusiness();

    /**
     * 根据商家名查询商家（模糊查询）
     * @return
     */
    @Select(" select * from business where businessName like concat('%',#{businessName},'%')")
    List<Business> listBusinessBykeyword(String businessName);

    @Insert("insert into business(businessName,businessAddress,businessExplain,businessImg,orderTypeId,starPrice,deliveryPrice,password) values (#{businessName},#{businessAddress} ,#{businessExplain},#{businessImg},#{orderTypeId},#{starPrice},#{deliveryPrice},#{password})")
    @Options(useGeneratedKeys=true, keyProperty="businessId", keyColumn="businessId")
    void registerBusiness(Business business);

    @Select("select * from business where businessId=#{businessId} and password=#{password}")
    Business getBusinessByIdByPass(@Param("businessId") String businessId, @Param("password") String password);

    @Update("update business set businessName=#{businessName},businessAddress=#{businessAddress}," +
            "businessExplain=#{businessExplain},businessImg=#{businessImg},orderTypeId=#{orderTypeId},starPrice=#{starPrice}," +
            "deliveryPrice=#{deliveryPrice},password=#{password} where businessId=#{businessId}")
    int updateBusiness(Business business);


    @Update("update business set totalSales=#{totalSales} where businessId=#{businessId}")
    int updateTotalSales(Business business);

    @Select("select * from business order by totalSales desc ")
    List<Business> sales_ranking();

    @Select("select b.* from business b left join food f on b.businessId=f.businessId where f.foodId=#{foodId}")
    Business getBusinessByFId(int foodId);

    @Select("select DISTINCT b.* from food f join business b on f.businessId=b.businessId where  f.foodName like concat('%',#{keyword},'%')")
    List<Business> getBusinessByFuzzy(String keyword);
}
